//
//  RankButtonViewController.m
//  WZLProduct
//
//  Created by boys on 16/3/24.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "RankButtonViewController.h"
#import "RankButtonTableViewCell.h"
/**< 定义全局const 为长宽高的单位变量 */
const CGFloat BackGroupHeight = 200;

@interface RankButtonViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) UITableView *aTableView;/**< 底层滚动的tableView */

@property (nonatomic, retain) UIImageView *imageBG; /**< 图片的背景颜色 */

@property (nonatomic, retain) UILabel *titleLabel;/**< 标题 */
@property (nonatomic, strong) UIView *backView; //导航栏的
@end

@implementation RankButtonViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _aTableView.delegate = self;
    _aTableView.dataSource = self;
    _aTableView.contentInset = UIEdgeInsetsMake(BackGroupHeight, 0, 0, 0);
    [self.view addSubview:_aTableView];
    
    // 设置大的图片
    self.imageBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, -BackGroupHeight, self.view.frame.size.width, self.view.frame.size.height)];
    _imageBG.image = [UIImage imageNamed:@"PlaceHolder_W_Ht"];
    [_aTableView addSubview:_imageBG];
    
    // 用label 设置标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 100, 30)];
    _titleLabel.text = @"titletitletitletitletitletitle";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor blueColor];
    [self.backView addSubview:_titleLabel];
    
    [_aTableView registerClass:[RankButtonTableViewCell class] forCellReuseIdentifier:@"RankButtonTableViewCell"];

    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    self.backView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.backView];
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor redColor];
    button.frame = CGRectMake(20, 30, 30, 30);
    [self.view addSubview:button];
    
}

#pragma mark - tableView Delegate & Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RankButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankButtonTableViewCell" forIndexPath:indexPath];
    
    
    return cell;
}


//控制 导航 和 隐藏 显示
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yOffset = scrollView.contentOffset.y;
    CGFloat xOffset = (yOffset + BackGroupHeight) * 2;
    if (yOffset < -BackGroupHeight) {
        CGRect rect = _imageBG.frame;
        rect.origin.y = yOffset;
        rect.size.height = - yOffset;
        rect.origin.x = xOffset;
        rect.size.width = self.view.frame.size.width + fabs(xOffset) * 2;
        _imageBG.frame = rect;
        
    }
    
    /** 随着图片拉伸隐藏title */
    CGFloat alpha = (yOffset + BackGroupHeight) / BackGroupHeight;
    
    _titleLabel.alpha = alpha;
    self.backView.alpha = alpha;

    
}










- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
